﻿<cfcomponent>
<cfscript>
DB = 'chinalaohu';
dbt = 'user_quiz';
</cfscript>
<cfparam name="UUID" default="0" type="string" >


<!--- 获取题目，因为要用到见鬼的随机抽取题目方法，一时半会儿不会啊！！！！ ，改用php调用 --->
<cffunction name="Q" access="remote" returntype="any" returnFormat="JSON">
<cfscript>
	j = {"success":"0"};
</cfscript>
<!---根据UUID查找ID--->
<cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" UUID="#uuid#"/>
<cfif uuidgetuid.recordcount neq 1>
	<cfscript>
		//"msg":"用户ID错误"
		j = {"success":"0","errCode":"0"};
	</cfscript>
<cfelse>
	<!--- 判断用户是否参与过答题 --->
    <cfquery datasource="#DB#" name="uque">
		select id from ac_question_user where acode='131202' and uid=#uuidgetuid.uid# and aresult=1
    </cfquery>
    <cfif uque.recordcount gt 0>
		<cfscript>
			//"msg":"用户答题次数超过"
			j = {"success":"1","isParticipate":"1","msg":"已经答过题"};
		</cfscript>
    <cfelse>
		<cfscript>
			httpRes = "";
		</cfscript>
		<cftry>
			<cfhttp url="http://adio.chinalaohu.com/app/php/acque.php?uid=#uuidgetuid.uid#" method="GET" result="result" charset="utf-8"></cfhttp>
		  <cfscript>
			httpRes = result.Filecontent;
		  </cfscript>
		  <cfcatch type="any">
		  </cfcatch>
		</cftry>
		<cfscript>
			j = {
				"success":"1",
				"isParticipate":"0",
				"quiz":DeserializeJSON(httpRes)
			};
		</cfscript>
    </cfif>
</cfif>
<cfscript>
	j = createObject("component",'app.cfc.cfjson').encode(j);
	return j;
</cfscript>
</cffunction>

<!--- 提交问题答案 --->
<cffunction name="Q2" access="remote" returntype="any" returnFormat="JSON">
  <cfscript>
	j = {"success":"0"};
	</cfscript>
	<!---根据UUID查找ID--->
  <cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" UUID="#uuid#"/>
  <cfif uuidgetuid.recordcount neq 1>
		<cfscript>
		//"msg":"用户ID错误"
    j = {"success":"0","errCode":"0"};
    </cfscript>
  <cfelse>
		<!---题目--->
    <cfquery datasource="#DB#" name="quiz">
    select * 
    from user_quiz
    where uqid = 1
    </cfquery>
    <!---需要加入答题时间段，为了测试暂没加--->
    <cfif quiz.recordcount eq 0>
			<cfscript>
      //"msg":"没有试题"
      j = {"success":"0","errCode":"-1"};
      </cfscript>
    <cfelse>
    
			<!---用户答题次数--->
      <cfquery datasource="#DB#" name="useranumber">
      select count(1) AS rc
      from user_quizanswer
      where uid = #uuidgetuid.uid# and uqID = 1
      </cfquery>
      <cfscript>
      //是否参加过
      if (useranumber.rc gt 0){
        isParticipate = 1;
      }
      else {
        isParticipate = 0;
      }
      </cfscript>
      
      <cfif useranumber.rc gte quiz.anumber>
        <cfscript>
        //"msg":"用户答题次数超过"
        j = {"success":"1","isParticipate":isParticipate};
        </cfscript>
      <cfelse>
        <cfscript>
        
        j = {
        "success":"1",
        "isParticipate":isParticipate,
        "quiz":DeserializeJSON(quiz.quiz_json)
        };
        
        </cfscript>
        
      </cfif>
    
    </cfif>
  
  </cfif>

	<cfscript>
	j = createObject("component",'app.cfc.cfjson').encode(j);
	return j;
	</cfscript>

</cffunction>

<!--- 答题提交，换了新表，原来以为是写死的，没插到数据库中 --->
<cffunction name="A" access="remote" returntype="any" returnFormat="json">
<cfscript>
	j = {"success":"0"};
</cfscript>
<cfif isdefined('UUID') AND isdefined('IMSI') AND isdefined('answers')>
	<cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" UUID="#uuid#"/>
	<cfif uuidgetuid.recordcount neq 1>
	  <cfscript>
		  //"msg":"用户ID错误"
		  j = {"success":"0","errCode":"0"};
	  </cfscript>
	<cfelse>
		<!--- 判断用户是否参与过答题 --->
		<cfquery datasource="#DB#" name="uque">
			select id from ac_question_user where acode='131202' and uid=#uuidgetuid.uid#
		</cfquery>
		<cfif uque.recordcount eq 0>
			<cfscript>
				j = {"success":"0","errCode":"-1","msg":"没找到获取题目时插入的记录"};
			</cfscript>
		<cfelse>
			<cftry>
				<cfquery datasource="#DB#" result="rs">
					update ac_question_user set aresult=1,answer='#answers#' where acode='131202' and uid = #uuidgetuid.uid#
				</cfquery>
				<cfscript>
					j = {"success":"1"};
				</cfscript>

				<cfcatch type="any">
					<cfscript>
						j = {"success":"0","errCode":"-1","msg":"其它原因错误"};
					</cfscript>
				</cfcatch>      
			</cftry>
		</cfif>  
	</cfif>
<cfelse>		
		<cfscript>
			//,"msg":"缺少参数"
			j = {"success":"0","errCode":"-1"};
		</cfscript>
</cfif>
<cfscript>
	j = createObject("component",'app.cfc.cfjson').encode(j);
	return j;
</cfscript>
</cffunction>

<cffunction name="A2" access="remote" returntype="any" returnFormat="json">
<cfscript>
	j = {"success":"0"};
</cfscript>
  
  <cfif isdefined('UUID') AND isdefined('IMSI') AND isdefined('answers')>
  	
    <cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" UUID="#uuid#"/>
    <cfif uuidgetuid.recordcount neq 1>
      <cfscript>
      //"msg":"用户ID错误"
      j = {"success":"0","errCode":"0"};
      </cfscript>
    <cfelse>
    	
      <!---这里需要限制用户答题次数，测试中暂无--->
    	<cftry>
        <cfquery datasource="#DB#" result="rs">
        insert into user_quizanswer(uqid,uid,IMSI,answers)
        value(1,#uuidgetuid.uid#,'#IMSI#','#answers#')
        </cfquery>
				<cfscript>
        j = {"success":"1"};
        </cfscript>
        
      	<cfcatch type="any">
					<cfscript>
          //数据库或其他原因出错
          j = {"success":"0","errCode":"-1"};
          </cfscript>
        </cfcatch>      
      </cftry>
      
    </cfif>    
  	
  <cfelse>
  	
		<cfscript>
		//,"msg":"缺少参数"
    j = {"success":"0","errCode":"-1"};
    </cfscript>
    
  </cfif>

	<cfscript>
	j = createObject("component",'app.cfc.cfjson').encode(j);
	return j;
	</cfscript>



</cffunction>




</cfcomponent>